using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ClientTools.Client.Controls.Stats
{
    public partial class ucStats : Client.Controls.BaseUserControl, Client.Controls.IUserControl
    {

        #region Private Variables
        private Library.Interfaces.IStats _ActStats;
        #endregion

        public ucStats()
        {
            InitializeComponent();
        }

        private void tsbGetStats_Click(object sender, EventArgs e)
        {
            this.GetStats();
        }

        private void cmdClearStatsByDate_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            global::ClientTools.Library.Stats.StatsInfo si;
            try
            {
                si = this._ActStats.DeleteStatsByDate(this.DateTimePicker1.Value);
            }
            catch (Exception ex)
            {
                this.ClearData();
                this.Enabled = false;
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.lblMessage1.Text = "Deleted " + si.Sessions.ToString() + " sessions with " + si.Entries.ToString() + " entries...";
            this.GetStats();

            this.Cursor = Cursors.Default;
        }

        public void ClearData()
        {
            this.Label1.Text = "-";
            this.lblMessage1.Text = "";
            this.pnlContent.Enabled = false;
        }

        private void GetStats()
        {
            this.Cursor = Cursors.WaitCursor;

            this.ClearData();
            global::ClientTools.Library.Stats.StatsInfo si;
            try
            {
                si = this._ActStats.GetStatsInfo();
            }
            catch (Exception ex)
            {
                this.ClearData();
                this.Enabled = false;
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.Label1.Text = "There are " + si.Sessions.ToString() + " sessions with " + si.Entries.ToString() + " entries in the database...";
            this.pnlContent.Enabled = true;

            this.Cursor = Cursors.Default;
        }

        #region I_UserControl Member

        public string Header
        {
            get { return "Statistics"; }
        }

        public void SetData(ClientTools.Client.Library.Settings.Profile profile)
        {
            this.ActProfile = profile;
            this.ActProfile.InitProvider();
            this.ActProvider = this.ActProfile.ProviderObject;

            if (!this.ImplementsStats())
            {
                this.Enabled = false;
                return;
            }

            this._ActStats = (Library.Interfaces.IStats)this.ActProvider;
            this.ClearData();
            this.Enabled = true;
        }

        public Image ButtonImage
        {
            get { return this.tsbStats.Image; }
        }

        public int Sort
        {
            get { return 3; }
        }

        public bool ShowDelimiter
        {
            get { return false; }
        }

        public string ButtonText
        {
            get { return "Statistics"; }
        }

        #endregion
    }
}
